home *** CD-ROM | disk | FTP | other *** search
/ New Star Software Collection / NSS_Collection.iso / 3-004 ms visual basic pro 30 / 4.imz / 4.IMA / QUERY.FR_ / QUERY.bin
Text File  |  1993-04-28  |  18KB  |  666 lines

  1. VERSION 2.00
  2. Begin Form fQuery 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Query Builder"
  6.    ClientHeight    =   5130
  7.    ClientLeft      =   1230
  8.    ClientTop       =   1155
  9.    ClientWidth     =   7095
  10.    ControlBox      =   0   'False
  11.    Height          =   5535
  12.    Icon            =   QUERY.FRX:0000
  13.    KeyPreview      =   -1  'True
  14.    Left            =   1170
  15.    LinkTopic       =   "Form1"
  16.    MaxButton       =   0   'False
  17.    MDIChild        =   -1  'True
  18.    ScaleHeight     =   5112
  19.    ScaleMode       =   0  'User
  20.    ScaleWidth      =   7116
  21.    Top             =   810
  22.    Width           =   7215
  23.    Begin PictureBox ExpressionBox 
  24.       BackColor       =   &H00C0C0C0&
  25.       Height          =   1092
  26.       Left            =   120
  27.       ScaleHeight     =   1065
  28.       ScaleWidth      =   6825
  29.       TabIndex        =   17
  30.       Tag             =   "OL"
  31.       Top             =   120
  32.       Width           =   6852
  33.       Begin CommandButton GetValuesButton 
  34.          Caption         =   "List Possible &Values"
  35.          Height          =   252
  36.          Left            =   4200
  37.          TabIndex        =   23
  38.          Top             =   720
  39.          Width           =   2292
  40.       End
  41.       Begin ComboBox cValue 
  42.          BackColor       =   &H00FFFFFF&
  43.          Height          =   288
  44.          Left            =   4080
  45.          Sorted          =   -1  'True
  46.          TabIndex        =   22
  47.          Text            =   "cValue"
  48.          Top             =   360
  49.          Width           =   2652
  50.       End
  51.       Begin ComboBox cOperator 
  52.          BackColor       =   &H00FFFFFF&
  53.          Height          =   288
  54.          Left            =   2880
  55.          Style           =   2  'Dropdown List
  56.          TabIndex        =   21
  57.          Top             =   360
  58.          Width           =   1092
  59.       End
  60.       Begin ComboBox cField 
  61.          BackColor       =   &H00FFFFFF&
  62.          Height          =   288
  63.          Left            =   120
  64.          Style           =   2  'Dropdown List
  65.          TabIndex        =   20
  66.          Top             =   360
  67.          Width           =   2652
  68.       End
  69.       Begin CommandButton ORButton 
  70.          Caption         =   "&Or into Criteria"
  71.          Height          =   252
  72.          Left            =   2040
  73.          TabIndex        =   19
  74.          Top             =   720
  75.          Width           =   1812
  76.       End
  77.       Begin CommandButton ANDButton 
  78.          Caption         =   "&And into Criteria"
  79.          Height          =   252
  80.          Left            =   120
  81.          TabIndex        =   18
  82.          Top             =   720
  83.          Width           =   1812
  84.       End
  85.       Begin Label OperatorLabel 
  86.          BackColor       =   &H00C0C0C0&
  87.          Caption         =   "Operator:"
  88.          Height          =   192
  89.          Left            =   2880
  90.          TabIndex        =   26
  91.          Top             =   120
  92.          Width           =   972
  93.       End
  94.       Begin Label ValueLabel 
  95.          BackColor       =   &H00C0C0C0&
  96.          Caption         =   "Value:"
  97.          Height          =   192
  98.          Left            =   4080
  99.          TabIndex        =   25
  100.          Top             =   120
  101.          Width           =   1452
  102.       End
  103.       Begin Label FieldNameLabel 
  104.          BackColor       =   &H00C0C0C0&
  105.          Caption         =   "Field Name:"
  106.          Height          =   192
  107.          Left            =   120
  108.          TabIndex        =   24
  109.          Top             =   120
  110.          Width           =   1332
  111.       End
  112.    End
  113.    Begin CommandButton JoinButton 
  114.       Caption         =   "Set Table &Joins"
  115.       Height          =   255
  116.       Left            =   4440
  117.       TabIndex        =   16
  118.       Top             =   2520
  119.       Width           =   2535
  120.    End
  121.    Begin ListBox cJoinFields 
  122.       BackColor       =   &H00FFFFFF&
  123.       Height          =   420
  124.       Left            =   4440
  125.       TabIndex        =   15
  126.       Tag             =   "OL"
  127.       Top             =   2760
  128.       Width           =   2535
  129.    End
  130.    Begin CommandButton CopySQLButton 
  131.       Caption         =   "&Copy SQL"
  132.       Height          =   375
  133.       Left            =   3000
  134.       TabIndex        =   14
  135.       Top             =   4680
  136.       Width           =   1095
  137.    End
  138.    Begin ComboBox cOrderByField 
  139.       BackColor       =   &H00FFFFFF&
  140.       Height          =   300
  141.       Left            =   4440
  142.       Style           =   2  'Dropdown List
  143.       TabIndex        =   12
  144.       Tag             =   "OL"
  145.       Top             =   2160
  146.       Width           =   2535
  147.    End
  148.    Begin ComboBox cGroupByField 
  149.       BackColor       =   &H00FFFFFF&
  150.       Height          =   300
  151.       Left            =   4440
  152.       Style           =   2  'Dropdown List
  153.       TabIndex        =   10
  154.       Tag             =   "OL"
  155.       Top             =   1560
  156.       Width           =   2535
  157.    End
  158.    Begin ListBox cTableList 
  159.       BackColor       =   &H00FFFFFF&
  160.       Height          =   1590
  161.       Left            =   120
  162.       MultiSelect     =   1  'Simple
  163.       TabIndex        =   9
  164.       Tag             =   "OL"
  165.       Top             =   1560
  166.       Width           =   1575
  167.    End
  168.    Begin CommandButton ShowSQLButton 
  169.       Caption         =   "&Show SQL"
  170.       Height          =   375
  171.       Left            =   1680
  172.       TabIndex        =   8
  173.       Top             =   4680
  174.       Width           =   1095
  175.    End
  176.    Begin ListBox cShowFields 
  177.       BackColor       =   &H00FFFFFF&
  178.       Height          =   1590
  179.       Left            =   1800
  180.       MultiSelect     =   1  'Simple
  181.       TabIndex        =   5
  182.       Tag             =   "OL"
  183.       Top             =   1560
  184.       Width           =   2535
  185.    End
  186.    Begin CommandButton CloseButton 
  187.       Cancel          =   -1  'True
  188.       Caption         =   "Close"
  189.       Height          =   375
  190.       Left            =   5640
  191.       TabIndex        =   2
  192.       Top             =   4680
  193.       Width           =   1095
  194.    End
  195.    Begin CommandButton RunQueryButton 
  196.       Caption         =   "&Run Query"
  197.       Height          =   375
  198.       Left            =   360
  199.       TabIndex        =   1
  200.       Top             =   4680
  201.       Width           =   1095
  202.    End
  203.    Begin CommandButton ClearButton 
  204.       Caption         =   "C&lear All"
  205.       Height          =   375
  206.       Left            =   4320
  207.       TabIndex        =   0
  208.       Top             =   4680
  209.       Width           =   1095
  210.    End
  211.    Begin TextBox cCriteria 
  212.       BackColor       =   &H00FFFFFF&
  213.       Height          =   1215
  214.       Left            =   120
  215.       MultiLine       =   -1  'True
  216.       ScrollBars      =   2  'Vertical
  217.       TabIndex        =   3
  218.       Tag             =   "OL"
  219.       Top             =   3360
  220.       Width           =   6855
  221.    End
  222.    Begin Label OrberByFieldLabel 
  223.       BackColor       =   &H00C0C0C0&
  224.       Caption         =   "Order By Field:"
  225.       Height          =   192
  226.       Left            =   4440
  227.       TabIndex        =   13
  228.       Top             =   1920
  229.       Width           =   2055
  230.    End
  231.    Begin Label GroupByFieldLabel 
  232.       BackColor       =   &H00C0C0C0&
  233.       Caption         =   "Group By Field:"
  234.       Height          =   192
  235.       Left            =   4440
  236.       TabIndex        =   11
  237.       Top             =   1320
  238.       Width           =   2055
  239.    End
  240.    Begin Label TableListLabel 
  241.       BackColor       =   &H00C0C0C0&
  242.       Caption         =   "Select Tables:"
  243.       Height          =   192
  244.       Left            =   120
  245.       TabIndex        =   7
  246.       Top             =   1320
  247.       Width           =   1455
  248.    End
  249.    Begin Label ShowFieldsLabel 
  250.       BackColor       =   &H00C0C0C0&
  251.       Caption         =   "Select Fields to Show:"
  252.       Height          =   192
  253.       Left            =   1800
  254.       TabIndex        =   6
  255.       Top             =   1320
  256.       Width           =   2055
  257.    End
  258.    Begin Label CriteriaLabel 
  259.       BackColor       =   &H00C0C0C0&
  260.       Caption         =   "Criteria:"
  261.       Height          =   180
  262.       Left            =   120
  263.       TabIndex        =   4
  264.       Top             =   3150
  265.       Width           =   1335
  266.    End
  267. End
  268.  
  269. Dim FShowSQL As Integer
  270. Dim FCopySQL As Integer
  271.  
  272. Sub ANDButton_Click ()
  273.   Dim f As Field
  274.  
  275.   If cField = "" Then Exit Sub
  276.  
  277.   Set f = gCurrentDB.TableDefs(stSTF((cField), 0)).Fields(stSTF((cField), 1))
  278.   
  279.   If cCriteria <> "" Then
  280.     cCriteria = cCriteria + Chr(13) + Chr(10) + "And "
  281.   End If
  282.   If f.Type = FT_STRING Or f.Type = FT_MEMO Or f.Type = FT_DATETIME Then
  283.     cCriteria = cCriteria + cField + " " + cOperator + " '" + cValue + "'"
  284.   Else
  285.     cCriteria = cCriteria + cField + " " + cOperator + " " + cValue
  286.   End If
  287.   cField.SetFocus
  288. End Sub
  289.  
  290. Sub cField_Click ()
  291.   cValue.Clear
  292. End Sub
  293.  
  294. Sub ClearButton_Click ()
  295.   cCriteria = ""
  296. End Sub
  297.  
  298. Sub CloseButton_Click ()
  299.   Unload Me
  300. End Sub
  301.  
  302. Sub CopySQLButton_Click ()
  303.   FCopySQL = True
  304.   Call RunQueryButton_Click
  305.   FCopySQL = False
  306. End Sub
  307.  
  308. Sub cTableList_Click ()
  309.   Dim i As Integer, ii As Integer
  310.   Dim t As TableDef
  311.   Dim st As String
  312.  
  313.   MsgBar "Updating Form Fields", True
  314.   cField.Clear
  315.   cShowFields.Clear
  316.   cGroupByField.Clear
  317.   cOrderByField.Clear
  318.   cValue.Clear
  319.  
  320.   cGroupByField.AddItem "(none)"
  321.   cOrderByField.AddItem "(none)"
  322.  
  323.   For ii = 0 To cTableList.ListCount - 1
  324.     If cTableList.Selected(ii) Then
  325.       Set t = gCurrentDB.TableDefs(cTableList.List(ii))
  326.       For i = 0 To t.Fields.Count - 1
  327.         st = cTableList.List(ii) + "." + t.Fields(i).Name
  328.         cField.AddItem st
  329.         cShowFields.AddItem st
  330.         cGroupByField.AddItem st
  331.         cOrderByField.AddItem st
  332.       Next
  333.     End If
  334.   Next
  335.   If cField.List(0) <> "" Then
  336.     cField.ListIndex = 0
  337.     cGroupByField.ListIndex = 0
  338.     cOrderByField.ListIndex = 0
  339.   End If
  340.   MsgBar "", False
  341.  
  342. End Sub
  343.  
  344. Sub Form_Load ()
  345.    On Local Error GoTo FLErr
  346.  
  347.    Dim ds As DynaSet
  348.    Dim i As Integer
  349.    Dim t As TableDef
  350.   
  351.    'Clear listbox
  352.    cCriteria = ""
  353.  
  354.    'Fill the Operator combo
  355.    cOperator.AddItem "="
  356.    cOperator.AddItem "<>"
  357.    cOperator.AddItem ">"
  358.    cOperator.AddItem ">="
  359.    cOperator.AddItem "<"
  360.    cOperator.AddItem "<="
  361.    cOperator.AddItem "Like"
  362.    cOperator.ListIndex = 0
  363.  
  364.    'fill the table list
  365.    For i = 0 To fTables.cTableList.ListCount - 1
  366.      cTableList.AddItem fTables.cTableList.List(i)
  367.    Next
  368.    cTableList.ListIndex = 0
  369.  
  370.    cValue = ""
  371.  
  372.   GoTo FLEnd
  373.  
  374. FLErr:
  375.   ShowError
  376.   Resume FLEnd
  377.  
  378. FLEnd:
  379.   Height = 5520
  380.   Width = 7224
  381.   Left = (VDMDI.Width - Width) / 2
  382.   Top = 0
  383.  
  384. End Sub
  385.  
  386. Sub Form_Paint ()
  387.   Outlines Me
  388.   PicOutlines ExpressionBox, cField
  389.   PicOutlines ExpressionBox, cOperator
  390.   PicOutlines ExpressionBox, cValue
  391. End Sub
  392.  
  393. Sub Form_Resize ()
  394.   On Error Resume Next
  395.  
  396.   If WindowState <> 1 Then
  397.     Height = 5520
  398.     Width = 7224
  399.   End If
  400. End Sub
  401.  
  402. Sub GetValuesButton_Click ()
  403.   Dim ds As DynaSet
  404.  
  405.   On Error GoTo GVErr
  406.  
  407.   MsgBar "Getting Possible Values", True
  408.   SetHourglass Me
  409.   Set ds = gCurrentDB.CreateDynaset("select Distinct " + (cField) + " from " + stSTF((cField), 0))
  410.   Do While ds.EOF = False
  411.     If Trim(ds(0)) <> "" Then
  412.       cValue.AddItem ds(0).Value
  413.     End If
  414.     ds.MoveNext
  415.   Loop
  416.   ds.Close
  417.   cValue = cValue.List(0)
  418.   cValue.SetFocus
  419.  
  420.   GoTo GVEnd
  421.  
  422. GVErr:
  423.   cValue = ""
  424.   Resume GVEnd
  425.  
  426. GVEnd:
  427.   ResetMouse Me
  428.   MsgBar "", False
  429.  
  430. End Sub
  431.  
  432. Sub JoinButton_Click ()
  433.   Dim i As Integer
  434.   Dim c As Integer
  435.  
  436.   For i = 0 To cTableList.ListCount - 1
  437.     If cTableList.Selected(i) = True Then
  438.       c = c + 1
  439.     End If
  440.   Next
  441.   If c < 2 Then
  442.     Beep
  443.     MsgBox "You Must Have at Least 2 Tables Selected!", 48
  444.   Else
  445.     MsgBar "Choose Joins", False
  446.     fJoin.Show MODAL
  447.     MsgBar "", False
  448.   End If
  449. End Sub
  450.  
  451. Sub ORButton_Click ()
  452.   Dim f As Field
  453.  
  454.   If cField = "" Then Exit Sub
  455.  
  456.   Set f = gCurrentDB.TableDefs(stSTF((cField), 0)).Fields(stSTF((cField), 1))
  457.  
  458.   If cCriteria <> "" Then
  459.     cCriteria = cCriteria + Chr(13) + Chr(10) + " Or "
  460.   End If
  461.   If f.Type = FT_STRING Or f.Type = FT_MEMO Or f.Type = FT_DATETIME Then
  462.     cCriteria = cCriteria + cField + " " + cOperator + " '" + cValue + "'"
  463.   Else
  464.     cCriteria = cCriteria + cField + " " + cOperator + " " + cValue
  465.   End If
  466.   cField.SetFocus
  467.  
  468. End Sub
  469.  
  470. Sub RunQueryButton_Click ()
  471.  
  472.   On Error GoTo OKErr
  473.  
  474.      Dim ds As DynaSet
  475.      Dim fs As String
  476.      Dim ts As String
  477.      Dim i As Integer
  478.  
  479.     MsgBar "Building Query", True
  480.      If cCriteria <> "" Then
  481.        stWhere$ = "AND " + LTrim(cCriteria)
  482.        'strip CRLFs
  483.        For i = 1 To Len(stWhere$)
  484.          If Mid(stWhere$, i, 1) = Chr$(13) Then
  485.            stTmp$ = stTmp$ + " "
  486.          ElseIf Mid(stWhere$, i, 1) = Chr$(10) Then
  487.            'do nothing
  488.          Else
  489.            stTmp$ = stTmp$ + Mid(stWhere$, i, 1)
  490.          End If
  491.        Next
  492.        stWhere$ = stTmp$
  493.  
  494.        stWhere$ = RTrim(stWhere$)
  495.      
  496.        'Add parens to stWhere$
  497.         stTmpWhere$ = stWhere$
  498.         Do
  499.           stTmp$ = stGetToken(stTmpWhere$, " ")
  500.           If fMatchParen% = False And UCase(stTmp$) = "AND" Then
  501.             stNewWhere$ = stNewWhere$ + stTmp$ + " ("
  502.             fMatchParen% = True
  503.           ElseIf fMatchParen% = True And UCase(stTmp$) = "AND" Then
  504.             stNewWhere$ = stNewWhere$ + ") " + stTmp$ + " ("
  505.             'fMatchParen% = False
  506.           Else
  507.             If UCase(stTmp$) = "OR" Or UCase(stTmp$) = "IN" Or UCase(stTmp$) = "LIKE" Then
  508.               stNewWhere$ = stNewWhere$ + " " + stTmp$ + " "
  509.             Else
  510.               stNewWhere$ = stNewWhere$ + stTmp$
  511.             End If
  512.           End If
  513.  
  514.         Loop Until stTmpWhere$ = ""
  515.         stWhere$ = stNewWhere$ + ")"
  516.  
  517.        'Build DynaSet string:
  518.        'Peel off leading AND/OR
  519.        If Mid(stWhere$, 2, 2) = "OR" Then
  520.          stWhere$ = Mid(stWhere$, 5, Len(stWhere$) - 5)
  521.        Else
  522.          stTmp$ = stGetToken(stWhere$, " ")
  523.        End If
  524.  
  525.        If stWhere$ <> "" Then
  526.          stWhere$ = " Where " + stWhere$
  527.        End If
  528.  
  529.      End If
  530.  
  531.      'check for join condition
  532.      If cJoinFields.ListCount > 0 Then
  533.        If stWhere$ = "" Then
  534.          stWhere$ = stWhere$ + " Where "
  535.        Else
  536.          stWhere$ = stWhere$ + " And "
  537.        End If
  538.        For i = 0 To cJoinFields.ListCount - 1
  539.          stWhere$ = stWhere$ + cJoinFields.List(i) + " And "
  540.        Next
  541.        stWhere$ = Mid(stWhere$, 1, Len(stWhere$) - 5)
  542.      End If
  543.      
  544.      'check for group by field
  545.      If cGroupByField <> "(none)" Then
  546.        stWhere$ = stWhere$ + " Group By " + cGroupByField
  547.      End If
  548.  
  549.      'check for order by field
  550.      If cOrderByField <> "(none)" Then
  551.        stWhere$ = stWhere$ + " Order By " + cOrderByField
  552.      End If
  553.  
  554.      'get show field names
  555.      For i% = 0 To cShowFields.ListCount - 1
  556.        If cShowFields.Selected(i%) Then
  557.          fs = fs + cShowFields.List(i%) + ","
  558.        End If
  559.      Next
  560.      If fs = "" Then
  561.        For i% = 0 To cTableList.ListCount - 1
  562.          If cTableList.Selected(i%) Then
  563.            fs = fs + cTableList.List(i%) + ".*,"
  564.          End If
  565.        Next
  566.        If fs = "" Then
  567.          fs = "*"
  568.        Else
  569.          fs = Mid(fs, 1, Len(fs) - 1)     'take off the last ","
  570.        End If
  571.      Else
  572.        fs = Mid(fs, 1, Len(fs) - 1)
  573.      End If
  574.  
  575.      'get table names
  576.      For i% = 0 To cTableList.ListCount - 1
  577.        If cTableList.Selected(i%) Then
  578.          ts = ts + cTableList.List(i%) + ","
  579.        End If
  580.      Next
  581.      ts = Mid(ts, 1, Len(ts) - 1)
  582.  
  583.      gstDynaString = "Select " + fs + " From " + ts + stWhere$
  584.          
  585.      If FShowSQL = False And FCopySQL = False Then
  586.        MsgBar "Running Query", True
  587.        gfFromSQL = True
  588.        'create a new dynaset form
  589.        If VDMDI.cSingleRecord = True Then
  590.          Dim dsform1 As New fDynaset
  591.          dsform1.Show
  592.        Else
  593.          Dim dsform2 As New fGridFrm
  594.          dsform2.Show
  595.        End If
  596.      ElseIf FShowSQL = True Then
  597.        MsgBar "", False
  598.        MsgBox gstDynaString, 0, "SQL Query"
  599.      ElseIf FCopySQL = True Then
  600.        fSQL.cSQLStatement = gstDynaString
  601.      End If
  602.  
  603.   GoTo OKEnd
  604.  
  605. OKErr:
  606.   If Err = 364 Then Resume OKEnd   'catch unloaded form
  607.   ShowError
  608.   Resume OKEnd
  609.  
  610. OKEnd:
  611.   MsgBar "", False
  612.  
  613. End Sub
  614.  
  615. Sub ShowSQLButton_Click ()
  616.   FShowSQL = True
  617.   Call RunQueryButton_Click
  618.   FShowSQL = False
  619. End Sub
  620.  
  621. Function stGetToken (stLn$, stDelim$) As String
  622.     On Error GoTo GetTokenError
  623.  
  624.     iOpenQuote% = InStr(1, stLn$, """")
  625.     iDelim% = InStr(1, stLn$, stDelim$)
  626.  
  627.     If (iOpenQuote% > 0) And (iOpenQuote% < iDelim%) Then
  628.          iCloseQuote% = InStr(iOpenQuote% + 1, stLn$, """")
  629.          iDelim% = InStr(iCloseQuote% + 1, stLn$, stDelim$)
  630.     End If
  631.  
  632.     If (iDelim% <> 0) Then
  633.          stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1, iDelim% - 1)))
  634.          stLn$ = Mid$(stLn$, iDelim% + 1)
  635.     Else
  636.          stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1)))
  637.          stLn$ = ""
  638.     End If
  639.  
  640.     If (Len(stToken$) > 0) Then
  641.          If (Mid$(stToken$, 1, 1) = """") Then
  642.               stToken$ = Mid$(stToken$, 2)
  643.          End If
  644.          If (Mid$(stToken$, Len(stToken$), 1) = """") Then
  645.               stToken$ = Mid$(stToken$, 1, Len(stToken$) - 1)
  646.          End If
  647.     End If
  648.     stGetToken = stToken$
  649.  
  650. GetTokenExit:
  651.     Exit Function
  652.  
  653. GetTokenError:
  654.     Resume GetTokenExit
  655. End Function
  656.  
  657. 'function to split the table and the field from a tbl.fld pair
  658. Function stSTF (tf As String, part As Integer) As String
  659.   If part = 0 Then
  660.     stSTF = Mid(tf, 1, InStr(1, tf, ".") - 1)
  661.   Else
  662.     stSTF = Mid(tf, InStr(1, tf, ".") + 1, Len(tf))
  663.   End If
  664. End Function
  665.  
  666.